package com.wanmait.recycle.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.wanmait.recycle.pojo.Admin;
import com.wanmait.recycle.pojo.Firsttype;
import com.wanmait.recycle.pojo.Rubbish;
import com.wanmait.recycle.pojo.RubbishUnit;
import com.wanmait.recycle.pojo.Secondtype;
import com.wanmait.recycle.pojo.Unit;
import com.wanmait.recycle.service.FirsttypeService;
import com.wanmait.recycle.service.RubbishService;
import com.wanmait.recycle.service.RubbishUnitService;
import com.wanmait.recycle.service.SecondtypeService;
import com.wanmait.recycle.util.UploadPhotosUtil;

@Controller
@RequestMapping("/manager")
public class RubbishController {
	
	@Resource
	private RubbishService rubbishService;
	@Resource
	private RubbishUnitService rubbishUnitService;
	@Resource
	private SecondtypeService secondtypeService;
	@Resource
	private FirsttypeService firsttypeService;
	@ModelAttribute("map")
	@RequestMapping("rubbish/rubbishAdd")
	public Map<String,Object> rubbishAddPage(){
		List<Secondtype> secondtypes=secondtypeService.findAll();
		List<Firsttype> firsttypes=firsttypeService.findAll();
		List<Unit> units=rubbishService.findAllUnits();
		Map<String, Object> infoMap = new HashMap<String,Object>();
		infoMap.put("secondtypes", secondtypes);
		infoMap.put("firsttypes", firsttypes);
		infoMap.put("units",units);
		return infoMap;
	}
	
	@ModelAttribute("map")
	@RequestMapping("rubbish/rubbishUpdate")
	public Map<String,Object> rubbishUpdatePage(Integer id){
		Rubbish object=rubbishService.selectById(id);
		List<Unit> runits=object.getUnits();
//		for(Unit unit:runits) {
//		System.out.println(unit.getId());}
																//runits等效于rubbishunits
		List<Unit> units=rubbishService.findAllUnits();
		List<RubbishUnit> rubbishUnits=rubbishUnitService.selectById(id);
		List<Secondtype> secondtypes=secondtypeService.findAll();
		List<Firsttype> firsttypes=firsttypeService.findAll();
		Map<String, Object> infoMap = new HashMap<String,Object>();
		infoMap.put("rubbishUnits", rubbishUnits);
		infoMap.put("secondtypes", secondtypes);
		infoMap.put("firsttypes", firsttypes);
		infoMap.put("units",units);
		infoMap.put("rubbish",object);
		return infoMap;
	}
	@RequestMapping("rubbish/rubbishList")
	public ModelAndView rubbishList(){
		
		PageInfo<Rubbish> pageInfo= rubbishService.findAllByPager();
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("pageInfo",pageInfo);
		
		return mv;
	}
	@RequestMapping("rubbish/rubbishListII")
	public ModelAndView rubbishListII(){
		
		PageInfo<Rubbish> pageInfo= rubbishService.findAllUnCheckByPager();
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("pageInfo",pageInfo);
		
		return mv;
	}
	@RequestMapping("rubbish/rubbishListIII")
	public ModelAndView rubbishListIII(){
		
		PageInfo<Rubbish> pageInfo= rubbishService.findAllPassByPager();
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("pageInfo",pageInfo);
		
		return mv;
	}
	@RequestMapping("rubbish/rubbishListVI")
	public ModelAndView rubbishListVI(){
		
		PageInfo<Rubbish> pageInfo= rubbishService.findAllUnPassByPager();
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("pageInfo",pageInfo);
		
		return mv;
	}
	@RequestMapping("rubbish/rubbishUpdateOne")
	public String rubbishUpLoad(Integer id,String rubbishName,Integer firsttypeId,Integer secondtypeId,@RequestParam(value="file",required=false) MultipartFile file,HttpServletRequest request,String content,String unitString) throws Exception{

		StringBuilder picsb=new StringBuilder();
		String pic;
		
		String photoName = UploadPhotosUtil.getPhotoNameAndSave("/static/rubbish_img", file, request);
		picsb.append(photoName);
		
		 pic=picsb.toString();
		
		HttpSession session=request.getSession();
		Admin admin=(Admin)session.getAttribute("admin");
		rubbishService.updateById(id,rubbishName,firsttypeId,secondtypeId,pic,content,admin,unitString);
		return "redirect:/manager/rubbish/rubbishList";
	}
	
	
	
	
	@RequestMapping("rubbish/rubbishChange")
	public String rubbishChange(Rubbish rubbish){
		rubbishService.update(rubbish);
		return "redirect:/manager/rubbish/rubbishList";
	}
	
	@RequestMapping("rubbish/rubbishChangeUpLoad")
	public String rubbishChangeUpLoad(Rubbish rubbish){
		rubbishService.update(rubbish);
	
		return "redirect:/manager/rubbish/rubbishList";
		
	}
	
@RequestMapping("rubbish/deleteSelect")
@ResponseBody
	public void rubbishDelete(String ids){
		
		rubbishService.deleteByIds(ids);
		
	}
@RequestMapping("rubbish/deleteOne")
@ResponseBody
	public void rubbishDelete(Integer id){
		
		rubbishService.deleteById(id);
		
	}
	
@RequestMapping("rubbish/recallOne")
@ResponseBody
	public void rubbishRecall(Integer id){
		
		rubbishService.recallById(id);
		
	}

@RequestMapping("rubbish/rubbishSearch")
public ModelAndView searchUserinfo(String searchStr, String beginTime, String endTime,String pageNum,String ftId) {
	if(ftId!=null&&!("".equals(ftId))) {
		Integer nftId=Integer.parseInt(ftId);
		ModelAndView mv=new ModelAndView("/manager/rubbish/rubbishSearchII");
		Map<String,String> searchMap=new HashMap<String,String>();
		mv.addObject("pageInfo", rubbishService.findAllByPrimarykey(nftId));
		mv.addObject("ftId",ftId);
		searchMap.put("searchStr",searchStr);
		searchMap.put("beginTime",beginTime);
		searchMap.put("endTime",endTime);
		mv.addObject("searchMap",searchMap);
		return mv;
	}
	
	if ("".equals(searchStr)) {
		searchStr = null;
	}
	if ( "".equals(beginTime)) {
		beginTime = null;
	}
	if ( "".equals(endTime)) {
		endTime = null;
	}
	ModelAndView mv=new ModelAndView();
	if(searchStr==null&&beginTime==null&&endTime==null&&pageNum==null) {
		return new ModelAndView("redirect:/manager/rubbish/rubbishList");
	}
	mv.addObject("pageInfo", rubbishService.findAllBySearch(searchStr, beginTime, endTime));
	Map<String,String> searchMap=new HashMap<String,String>();
	searchMap.put("searchStr",searchStr);
	searchMap.put("beginTime",beginTime);
	searchMap.put("endTime",endTime);
	mv.addObject("searchMap",searchMap);
	return mv;
	
}	
@RequestMapping("/rubbish/rubbishAddOne")
public String addRubbish(String rubbishName,Integer firsttypeId,Integer secondtypeId,@RequestParam(value="file",required=false) MultipartFile file,HttpServletRequest request,String content,String unitString)throws Exception {
	
	
	
	StringBuilder picsb=new StringBuilder();
	
		String photoName = UploadPhotosUtil.getPhotoNameAndSave("/static/rubbish_img", file, request);
		picsb.append(photoName);
	
	String pic=picsb.toString();
	HttpSession session=request.getSession();
	Admin admin=(Admin)session.getAttribute("admin");
	rubbishService.addRubbish(rubbishName,firsttypeId,secondtypeId,pic,content,admin,unitString);
	return "redirect:/manager/rubbish/rubbishList";
}
}
